/**
 * @NApiVersion 2.x
 * @NScriptType MapReduceScript
 */
define(['N/search', 'N/record'], function (search, record) {
    function getInputData() {
        //查找  没有发货,开票,付款的 亚马逊订单 亚马逊销售单,
        return search.create({
            type: 'salesorder',
            filters: [['mainline', 'is', 'T']
                , 'and', ['status', 'is', 'SalesOrd:B']
                , 'and', ['custbody_platform_source', 'is', 1]]
        }).run().getRange(0, 100);
        //如果search  没有结果 ,则不会执行 map!
    }

    function map(context) {
        var itemFulfillmentId, invoiceId, paymentId;
        try {
            var soId = JSON.parse(context.value).id;
            log.debug('soId', soId);
            var accountId = getAccountIdBySoId(soId);
            log.debug('accountId', accountId);
            itemFulfillmentId = itemFulfillment(soId);
            log.debug('create itemFulfillment', itemFulfillmentId);
            invoiceId = invoice(soId);
            log.debug('create invoice', invoiceId);
            try {
                paymentId = customerPayment(invoiceId, accountId);
                log.debug('create payment', paymentId);
            }catch (e) {
                log.debug('e',JSON.stringify(e));
            }


        } catch (e) {
            //发生错误,删除单据
            if (paymentId) {
                record.delete({
                    type: record.Type.CUSTOMER_PAYMENT,
                    id: paymentId
                });
            }
            if (invoiceId) {
                record.delete({
                    type: record.Type.INVOICE,
                    id: paymentId
                });
            }
            if (itemFulfillmentId) {
                record.delete({
                    type: record.Type.ITEM_FULFILLMENT,
                    id: paymentId
                });
            }
            log.debug('e', e);
        }
    }

    function reduce(context) {
    }

    function summarize(summary) {
    }


    //发货
    function itemFulfillment(recId) {
        var itemFulfillmentRecord = record.transform({
            fromType: record.Type.SALES_ORDER,
            fromId: recId,
            toType: record.Type.ITEM_FULFILLMENT,
            isDynamic: true
        });
        //发货状态  Shipped   C
        itemFulfillmentRecord.setValue('shipstatus', 'C');
        return itemFulfillmentRecord.save();
    }

    //开票
    function invoice(recId) {
        var invoiceRecord = record.transform({
            fromType: record.Type.SALES_ORDER,
            fromId: recId,
            toType: record.Type.INVOICE,
            isDynamic: true
        });
        return invoiceRecord.save();
    }

    //收款
    function customerPayment(invoiceId, accountId) {
        var customerPaymentRecord = record.transform({
            fromType: record.Type.INVOICE,
            fromId: invoiceId,
            toType: record.Type.CUSTOMER_PAYMENT,
            isDynamic: true
        });
        //亚马逊应收账款
        customerPaymentRecord.setValue('account', accountId);
        return customerPaymentRecord.save();
    }

    //获得收款账户
    function getAccountIdBySoId(soId) {
        var soRecord = record.load({
            type: 'salesorder',
            id: soId,
            isDynamic: true
        });
        var storeId = soRecord.getValue('custbody_platform_store');
        log.debug('storeId', storeId);
        var storeRecord = record.load({
            type: 'customrecord_platform_store_information',
            id: storeId,
            isDynamic: true
        });
        return storeRecord.getValue('custrecord_account');
    }

    return {
        getInputData: getInputData,
        map: map,
        reduce: reduce,
        summarize: summarize
    };
});